package Project;

import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Dinamic extends JFrame implements ActionListener {
	JFrame jf = null;
	JPanel jp = new JPanel();
	JPanel jp1 = new JPanel();
	JPanel jp2;
	JButton btn_join = null;
	JButton btn_cancle = null;
	JTextField f;
	JTextField f1;
	JLabel lb_name = new JLabel("이름 : ");
	JLabel lb_dept = new JLabel("학과 : "); ;
	JLabel lb_year = new JLabel("학년 : ");
	JLabel lb_num = new JLabel("학번 : ");
	JLabel lb_remove = new JLabel();
	JLabel lb_remove1 = new JLabel();
	JTextField tf_name = new JTextField(5);
	JTextField tf_dept = new JTextField(5);
	JTextField tf_year = new JTextField(5); 
	JTextField tf_num = new JTextField(5);
	Checkbox cb_stu, cb_pro;
	CheckboxGroup cg;
	JPanel ljp, southjp;
	boolean state=true;
	
	public Dinamic(Point p){
		jf = new JFrame();
		jf.setLocation(p);
		
		btn_join = new JButton("가입");
		btn_join.addActionListener(this);
		btn_cancle = new JButton("취소");
		btn_cancle.addActionListener(this);
		southjp = new JPanel();
		
		cg = new CheckboxGroup();
		cb_stu = new Checkbox("학생", cg, true);
		cb_pro = new Checkbox("교수", cg, false);
		cb_stu.addItemListener(new EventHandler());
		cb_pro.addItemListener(new EventHandler());
		
		f = new JTextField(5);
		f1 = new JTextField(5);
		
		jp1.setLayout(new GridLayout(4, 2));
		jp1.add(lb_num);
		jp1.add(tf_num);
		jp1.add(lb_name);
		jp1.add(tf_name);
		jp1.add(lb_dept);
		jp1.add(tf_dept);
		jp1.add(lb_year);
		jp1.add(tf_year);
		jf.add(jp1, "Center");
		
		southjp.add(btn_join);
		southjp.add(btn_cancle);
		
		jf.add(jp1, "Center");
		jf.add(southjp, "South");
		jp2 = new JPanel();
		jp2.add(cb_stu);
		jp2.add(cb_pro);
		
		// jp1.add(b);
		// jp1.add(c);
		
		jf.add(jp2,"North");
		// jf.add(jp1, "South");
		
		jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
		jf.setSize(500, 300);
		jf.setVisible(true);
	}
	public void actionPerformed(ActionEvent evt){
		if(evt.getSource().equals(btn_join)){
			DAOTest test = new DAOTest();
			String dept = this.tf_dept.getText();
			String year = this.tf_year.getText();
			String stunum = this.tf_num.getText();
			String name = this.tf_name.getText();
			
			test.Join(dept, year, stunum, name, state);
		}
		if(evt.getSource().equals(btn_cancle)){
			Point p = jp.getLocation();
			jf.setVisible(false);
			LoginUi ui = new LoginUi();
			ui.LoginUI(p);
			ui.uiframe();
		}
	}
	class EventHandler implements ItemListener{
		public void itemStateChanged(ItemEvent e){
			Checkbox cb = (Checkbox)e.getSource();
			String sel = cb.getLabel();
			if(sel.equals("학생")){
				lb_num.setText("학번 : ");
				jp1.setLayout(new GridLayout(4,2));
				jp1.add(lb_year);
				jp1.add(tf_year);
				jp1.remove(lb_remove);
				jp1.remove(lb_remove1);
				jf.add(jp1, "Center");
				jf.validate();
				state = true;
				System.out.print("상태 : ");
				System.out.println(state);
			}else{
				lb_num.setText("교수번호 : ");
				jf.add(jp1, "Center");
				jp1.setLayout(new GridLayout(3,2));
				jp1.remove(lb_year);
				jp1.remove(tf_year);
				
				jf.add(jp1, "Center");
				jf.validate();
				state = false;
				System.out.print("상태 : ");
				System.out.println(state);
			}
		}
	}

}
